package cn.itsource.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author : 张金铭
 * @description :
 * @create :2022-11-07 13:43:00
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> {
    private T data; //数据

    private Integer code = PetHomeConstant.SYSTEM_SUCCESS;//状态码 ,默认为0000表示没问题

    private String message = "OK";//传送信息,默认为OK

    private Boolean success = true;//操作是否成功,默认成功

    /**
     * 操作成功
     * @return
     */
    public static JsonResult success(){
        return new JsonResult();
    }


    /**
     * 操作成功传输一个信息
     * @param msg
     * @return
     */
    public static JsonResult success(String msg){
        JsonResult result = new JsonResult();
        result.setMessage(msg);
        return result;
    }

    /**
     * 操作成功传输一个信息和数据
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static<T> JsonResult<T> success(String msg,T data){
        JsonResult result = new JsonResult();
        result.setMessage(msg);
        result.setData(data);
        return result;
    }


    /**
     * 操作成功，只返回信息
     * @param data
     * @param <T>
     * @return
     */
    public static<T> JsonResult<T> success(T data){
        JsonResult<T> result = new JsonResult<>();
        result.setData(data);
        return result;
    }

    /**
     * 错误，返回一个错误信息
     * @param msg
     * @return
     */
    public static JsonResult error(String msg){
        JsonResult result = new JsonResult();
        result.setMessage(msg);
        result.setSuccess(false);
        return result;
    }

    /**
     *
     * @param code
     * @return
     */
//    public static JsonResult error(Integer code){
//        JsonResult result = new JsonResult();
//        result.setCode(code);
//        result.setSuccess(false);
//        return result;
//    }

    /**
     * 错误，返回信息和代码
     * @param msg
     * @param code
     * @return
     */
    public static JsonResult error(String msg,Integer code){
        JsonResult result = new JsonResult();
        result.setMessage(msg);
        result.setCode(code);
        result.setSuccess(false);
        return result;
    }




}
